-- this is the logic of elimination
-- may this is just a interface

-- every elem data struct
local cubeElem = {}
cubeElem.__index = cubeElem

function cubeElem.new()
  local obj = {}
  -- obj.tex = nil
  obj.info = 0

  return setmetatable(obj, cubeElem)
end

function cubeElem.new(info)
  local obj = {}
  -- obj.tex = nil
  obj.info = info

  return setmetatable(obj, cubeElem)
end

function cubeElem:setInfo(info)
  self.info = info
end


local quene = {}
quene.__index = quene

function quene.new()
  local obj = {}
  obj.elems = {}

  return setmetatable(obj, quene)
end

function quene:addElem(elem)
  self.elems[#self.elems+1] = elem
end

-- ------------ Interface !!!!------------
-- param no : the No. of quene that be clicked up
-- return : 1, Continuous count of the same cube 
--          2, the cube elem data
function quene:checkHit(no)
  local elems = self.elems
  local hitElem = elems[no]

  local rangeLeft = no
  local rangeRight = no
  local count = 0
  local tmpNo = 1
  local tmpElem
  while elems[no - tmpNo] ~= nil do
    local tmpElem = elems[no-tmpNo]
    if tmpElem.info == hitElem.info then
      count = count + 1
      rangeLeft = no-tmpNo
    else
      break
    end
    tmpNo = tmpNo + 1
  end

  tmpNo = 1
  tmpElem = nil
  while elems[no + tmpNo] ~= nil do
    local tmpElem = elems[no+tmpNo]
    if tmpElem.info == hitElem.info then
      count = count + 1
      rangeRight = no + tmpNo
    else
      break
    end
    tmpNo = tmpNo + 1
  end

  return rangeLeft, rangeRight, hitElem
end

function quene:checkHitAndRemoveSameCube(no)
  local rangeLeft, rangeRight, hitElem = self:checkHit(no)
  -- remove the same cube
  for i = rangeRight, rangeLeft, -1 do
    -- self.elems[i] = nil
    table.remove(self.elems, i)
  end
end

function quene:print()
  print("------\n" .. #self.elems .. "\n------\n")
  for i = 1, #self.elems do
    print(self.elems[i].info)
  end
  print("@@@@@@@@")
end


-- test 
local queneUI = quene.new()
queneUI:addElem(cubeElem.new(1))
queneUI:addElem(cubeElem.new(2))
queneUI:addElem(cubeElem.new(2))
queneUI:addElem(cubeElem.new(1))
queneUI:addElem(cubeElem.new(1))
queneUI:addElem(cubeElem.new(1))
queneUI:addElem(cubeElem.new(4))
queneUI:addElem(cubeElem.new(3))
queneUI:addElem(cubeElem.new(3))

queneUI:print()
queneUI:checkHitAndRemoveSameCube(4)
-- print(queneUI:checkHit(4))
queneUI:print()
